import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:mall/constant/app_colors.dart';
import 'package:mall/constant/app_strings.dart';
import 'package:mall/constant/text_style.dart';
import 'package:mall/gen/r.dart';
import 'package:mall/modules/model/address_entity.dart';
import 'package:mall/modules/sc/address_sc.dart';
import 'package:mall/ui/page/mine/address/widgets/placeholder_in_address_widget.dart';
import 'package:mall/utils/image_component/image_component.dart';
import 'package:mall/utils/list_placeholder_component/list_placeholder_component.dart';
import 'package:mall/router/navigator_util.dart';
import 'package:mall/utils/refresh/refresh_widget.dart';
import 'package:mall/utils/refresh/warp_refresh_controller.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class AddressViewPage extends StatefulWidget {

  AddressViewPage();

  @override
  _AddressViewPageState createState() => _AddressViewPageState();
}

class _AddressViewPageState extends State<AddressViewPage> {
  final sc_addressList = Get.put(AddressListSc());
  late final _refreshController = WarpRefreshController(loadState: sc_addressList.loadState);

  @override
  void dispose() {
    super.dispose();
    _refreshController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('地址管理'),
        centerTitle: true,
        actions: <Widget>[
          InkWell(
              onTap: () => _goAddressEdit(null),
              child: Container(
                margin: EdgeInsets.only(right: ScreenUtil().setWidth(10.0)),
                alignment: Alignment.center,
                child: Text(AppStrings.ADD_ADDRESS),
              ))
        ],
      ),
      body: Obx(
        () => RefreshComponent(
          controller: _refreshController,
          // onLoading: () => sc_addressList.loadMoreData(),
          onRefresh: () => sc_addressList.refreshData(),
          child: WrappedListPlaceholderComponent.widget(
            emptyWidget: PlaceholderInAddressWidget(),
            sc: sc_addressList,
            builder: (p0) => ListView.builder(
                padding: EdgeInsets.only(top: 10, left: 15, right: 15),
                itemCount: sc_addressList.data.length,
                itemBuilder: (BuildContext context, int index) {
                  return _addressItemView(sc_addressList.data[index]);
                }),
          ),
        ),
      ),
    );
  }

  Widget _addressItemView(AddressList addressData) {
    return Card(
        color: AppColors.C_FFFFFF,
        child: Container(
          padding: EdgeInsets.all(15),
          child: InkWell(
            highlightColor: AppColors.C_FFFFFF,
            splashColor: AppColors.C_FFFFFF,
            onTap: () => _goFillInOrder(addressData),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                //头像
                ClipRRect(
                  borderRadius: BorderRadius.circular(22.5),
                  child: Container(
                    alignment: Alignment.center,
                    color: Colors.grey,
                    width: 45,
                    height: 45,
                    child: Text(addressData.name?.substring(0, 1) ?? "", style: FMTextStyle.color_ffffff_size_22),
                  ),
                ),
                SizedBox(width: 10),
                Expanded(
                    child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    //名称和电话
                    Row(
                      children: <Widget>[
                        Text(
                          addressData.name ?? "",
                          style: FMTextStyle.color_222222_size_13,
                        ),
                        SizedBox(width: 10),
                        Text(
                          addressData.tel ?? "",
                          style: FMTextStyle.color_999999_size_13,
                        ),
                      ],
                    ),
                    SizedBox(height: 5),
                    //地址
                    Text(
                      (addressData.province ?? "") +
                          (addressData.city ?? "") +
                          (addressData.county ?? "") +
                          (addressData.addressDetail ?? ""),
                      maxLines: 3,
                      overflow: TextOverflow.ellipsis, // 显示不完，就在后面显示点点
                      style: FMTextStyle.color_222222_size_13,
                    )
                  ],
                )),

                //编辑
                InkWell(
                    highlightColor: AppColors.C_FFFFFF,
                    splashColor: AppColors.C_FFFFFF,
                    onTap: () => _goAddressEdit(addressData.id),
                    child: Container(
                      width: 50,
                      height: 45,
                      child: Center(
                        child: ImageComponent(
                          R.image.addressEdit.path,
                          width: 13,
                          height: 15,
                        ),
                      ),
                    ))
              ],
            ),
          ),
        ));
  }

  _goFillInOrder(AddressList addressData) {
    final callBack = Get.arguments['callback'];
    if (callBack != null) {
      callBack(addressData);
      Get.back();
    } else{
       NavigatorUtil.goAddressEdit(addressData.id);
    }
  }

  _goAddressEdit(int? addressId) {
    NavigatorUtil.goAddressEdit(addressId);
  }
}
